package com.jueling.culture.util;


import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    private static final String FILE_NAME = "觉灵文化{typeName}.xls";

    public static void exportExcel(List<List<Object>> list, List<String> channels, HttpServletResponse response, String typeName) {
        //获取项目相对路径
        String path = System.getProperty("user.dir");
        //时间戳
        Long date = System.currentTimeMillis();
        String replace = FILE_NAME.replace("{typeName}", typeName);
        //xls临时文件名称
        String tempFileName = FILE_NAME.replace("{typeName}", typeName);
        //xls名称
        String allFileName = FILE_NAME.replace("{typeName}", typeName);

        //模板名称
        String templateName = "template.xls";
        OutputStream outputStream = null;
        FileInputStream fi = null;
        OutputStream os = null;
        File file = null;
        try {
            file = new File(path + File.separator + tempFileName);

            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            outputStream = new FileOutputStream(file);

            //获取表头集合
            List<String> headers = new ArrayList<>();
            headers.add("序号");
            for (String head : channels) {
                headers.add(head);
            }
            Context context = new Context();
            context.putVar("list", list);
            context.putVar("headers", headers);
            context.putVar("tableName",allFileName.substring(0,allFileName.length()-4));

            Resource resource = new ClassPathResource("templates/" + templateName);
            InputStream is = resource.getInputStream();

            JxlsHelper helper = new JxlsHelper();


            helper.setDeleteTemplateSheet(true);
            helper.setHideTemplateSheet(true);
            helper.processTemplate(is, outputStream, context);

            //读取文件，并返回
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(allFileName, "UTF-8"));

            fi = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fi);
            os = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = bis.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (fi != null) {
                    fi.close();
                }
                if (os != null) {
                    os.close();
                }
                if (file != null) {
                    file.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
